package org.teziyou.ticket.web;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.teziyou.ticket.constant.CommonConstant;
import org.teziyou.ticket.interceptor.LoginInterceptor;
import org.teziyou.ticket.model.Dict;
import org.teziyou.ticket.model.OnSaleTicket;
import org.teziyou.ticket.model.ScenicComment;
import org.teziyou.ticket.model.ScenicSpot;
import org.teziyou.ticket.model.Ticket;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.EncryptionKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

public class TicketController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TicketController.class);
	
	/**
	 * 票务详情页
	 */
	public void detail() {
		String oid = getPara();
		OnSaleTicket ost = OnSaleTicket.dao.findById(oid);
		
		long tid = ost.getLong("ticket_info_id");
		Ticket t = Ticket.dao.findById(tid);
		
		long sid = t.getLong("scenic_id");
		ScenicSpot ss = ScenicSpot.dao.findById(sid);
		
		int dictLevelId = ss.getInt("dic_scenic_level");
		Dict levelDict = Dict.dao.getDictById(dictLevelId);
		logger.info(levelDict);
		ss.put("levelDict", levelDict);
		
		Page<ScenicComment> commentPage = ScenicComment.dao.getScenicCommentsByPage(sid, 1, CommonConstant.pagesize);
		List<Record> summary = ScenicComment.dao.summaryComments(sid);
		
		List<Dict> starList = Dict.dao.getDictsByItemType(CommonConstant.dictCommentStar);
		logger.info(starList);
		List<Dict> serviceList = Dict.dao.getDictsByItemType(CommonConstant.dictCommentService);
		logger.info(serviceList);
		List<Dict> trafficList = Dict.dao.getDictsByItemType(CommonConstant.dictCommentTraffic);
		logger.info(trafficList);
		
		setAttr("ost", ost);
		setAttr("t", t);
		setAttr("ss", ss);
		setAttr("cPage", commentPage);
		setAttr("cSum", summary);
		setAttr("starList", starList);
		setAttr("serviceList", serviceList);
		setAttr("trafficList", trafficList);
		setAttr("sid", sid);
		
		renderJsp("detail.jsp");
	}
	
	/**
	 * 评论分页
	 */
	public void commentPage() {
		long sid = getParaToLong(0);
		int pageNo = getParaToInt(1);
		Page<ScenicComment> commentPage = ScenicComment.dao.getScenicCommentsByPage(sid, pageNo, CommonConstant.pagesize);
		List<Record> summary = ScenicComment.dao.summaryComments(sid);
		
		setAttr("cPage", commentPage);
		setAttr("cSum", summary);
		
		renderJsp("commentPage.jsp");
	}
	
	/**
	 * 提交评论
	 */
	@Before(LoginInterceptor.class)
	public void submitComment() {
		ScenicComment sc = getModel(ScenicComment.class);
		String vcode = sc.getStr("vcode");
		vcode = EncryptionKit.md5Encrypt(vcode);
		String cookieVcode = super.getCookie(CommonConstant.comment_captcha_key);
		if(!vcode.equals(cookieVcode))
			renderJson(super.fail("验证码错误！！"));
		else {
			boolean result = Db.tx(new IAtom() {
				@Override
				public boolean run() throws SQLException {
					
					return false;
				}
			});
		}
	}

}
